﻿@model WebApp.Models.S_department

@{
    Layout = "~/Views/Shared/_DialogLayout.cshtml";
}

@section css{
    <style type="text/css">
    .span-color{
        display: inline-block;
        width:16px;height:16px;
        border:1px solid #ddd;
        vertical-align: middle;
        margin: 3px 5px 3px 2px;
    }
    .dropdown-menu{
        padding: 5px;
        width: 190px;
    }
    .dropdown-menu>li{
        float:left;
    }
    .dropdown-menu>li>a{
        float:left;
        display:inline-block;
        padding: 5px 3px 5px 5px;
        margin: 2px;
    }
    </style>
}

@using (Html.BeginForm("", "", FormMethod.Post, new { @class = "form-horizontal form-inline", id = "inputform" }))
{
    <div class="dialog-content">
        @Html.AntiForgeryToken()
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

        <input type="hidden" id="Id" value="@Model.Id" />
        <input type="hidden" value="@(Model.Updatetime.HasValue ? Model.Updatetime.Value.ToString("yyyy-MM-dd HH:mm:ss.fff") : "")" class="form-control datafield" data-fieldname="Updatetime" />
        <input type="hidden" value="@Model.Depttype" class="form-control datafield" data-fieldname="Depttype" />

        <div class="row">
            <div class="col-xs-12 nopadding" data-autosize="true">
                <div class="widget-box">
                    <div class="widget-title">
                        <h5>机关科室信息</h5>
                    </div>
                    <div class="widget-content">
                        <table cellpadding="3" cellspacing="0" border="0" class="inputform">
                            <tr>
                                <td class="td-label"><label for="Roleid">排序</label></td>
                                <td class="td-control">
                                    @Html.TextBox("Sort", @Model.Sort, new { @class = "form-control datafield", @data_fieldname = "Sort" })
                                </td>
                            </tr>
                            <tr>
                                <td class="td-label"><label for="Rolename">科室名称</label></td>
                                <td class="td-control" style="width:300px;">
                                    @Html.TextBox("Deptname", @Model.Deptname, new { @class = "form-control datafield", @data_fieldname = "Deptname" })
                                </td>
                            </tr>
                            <tr>
                                <td class="td-label"><label for="Rolename">背景色</label></td>
                                <td class="td-control" style="width:300px;">
                                    @Html.Hidden("Bgcolor", Model.Bgcolor, new { @class = "form-control datafield", @data_fieldname = "Bgcolor" })
                                    <div class="btn-group" id="colorPicker">
                                        <button data-toggle="dropdown" class="btn btn-xs btn-default dropdown-toggle">
                                            <span class="span-color" style="background:@Model.Bgcolor;"></span><span class="caret"></span>
                                        </button>
                                        <ul class="dropdown-menu">
                                            <li><a href="#" data-color="#ffff00"><span class="span-color" style="background:#ffff00;"></span></a></li>
                                            <li><a href="#" data-color="#ffc000"><span class="span-color" style="background:#ffc000;"></span></a></li>
                                            <li><a href="#" data-color="#92d050"><span class="span-color" style="background:#92d050;"></span></a></li>
                                            <li><a href="#" data-color="#00b0f0"><span class="span-color" style="background:#00b0f0;"></span></a></li>
                                            <li><a href="#" data-color="#9966ff"><span class="span-color" style="background:#9966ff;"></span></a></li>

                                            <li><a href="#" data-color="#99ff99"><span class="span-color" style="background:#99ff99;"></span></a></li>
                                            <li><a href="#" data-color="#aeaaaa"><span class="span-color" style="background:#aeaaaa;"></span></a></li>
                                            <li><a href="#" data-color="#ffe699"><span class="span-color" style="background:#ffe699;"></span></a></li>
                                            <li><a href="#" data-color="#b4c6e7"><span class="span-color" style="background:#b4c6e7;"></span></a></li>
                                            <li><a href="#" data-color="#6699ff"><span class="span-color" style="background:#6699ff;"></span></a></li>

                                            <li><a href="#" data-color="#ff99ff"><span class="span-color" style="background:#ff99ff;"></span></a></li>
                                            <li><a href="#" data-color="#ffcc99"><span class="span-color" style="background:#ffcc99;"></span></a></li>
                                            <li><a href="#" data-color="#99ffcc"><span class="span-color" style="background:#99ffcc;"></span></a></li>
                                            <li><a href="#" data-color="#009999"><span class="span-color" style="background:#009999;"></span></a></li>
                                            <li><a href="#" data-color="#e7e6e6"><span class="span-color" style="background:#e7e6e6;"></span></a></li>

                                            <li><a href="#" data-color="#ffffb5"><span class="span-color" style="background:#ffffb5;"></span></a></li>
                                            <li><a href="#" data-color="#ffedb8"><span class="span-color" style="background:#ffedb8;"></span></a></li>
                                            <li><a href="#" data-color="#dbffb5"><span class="span-color" style="background:#dbffb5;"></span></a></li>
                                            <li><a href="#" data-color="#b4ebff"><span class="span-color" style="background:#b4ebff;"></span></a></li>
                                            <li><a href="#" data-color="#cfb6ff"><span class="span-color" style="background:#cfb6ff;"></span></a></li>

                                            @*<li><a href="#" data-color="#ffb5ba"><span class="span-color" style="background:#ffb5ba;"></span></a></li>
                                            <li><a href="#" data-color="#ffd6b4"><span class="span-color" style="background:#ffd6b4;"></span></a></li>
                                            <li><a href="#" data-color="#d2e0da"><span class="span-color" style="background:#d2e0da;"></span></a></li>
                                            <li><a href="#" data-color="#b5ffff"><span class="span-color" style="background:#b5ffff;"></span></a></li>
                                            <li><a href="#" data-color="#fabfff"><span class="span-color" style="background:#fabfff;"></span></a></li>

                                            <li><a href="#" data-color="#e5e5a3"><span class="span-color" style="background:#e5e5a3;"></span></a></li>
                                            <li><a href="#" data-color="#e5d5a5"><span class="span-color" style="background:#e5d5a5;"></span></a></li>
                                            <li><a href="#" data-color="#c5e5a3"><span class="span-color" style="background:#c5e5a3;"></span></a></li>
                                            <li><a href="#" data-color="#a3d4e5"><span class="span-color" style="background:#a3d4e5;"></span></a></li>
                                            <li><a href="#" data-color="#b9a3e5"><span class="span-color" style="background:#b9a3e5;"></span></a></li>*@

                                        </ul>
                                    </div>
                                </td>
                            </tr>
                            <tr>
                                <td class="td-label"><label for="Description">备注</label></td>
                                <td class="td-control">
                                    @Html.TextArea("Remark", @Model.Remark, new { @class = "form-control datafield", @data_fieldname = "Remark" })
                                </td>
                            </tr>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="dialog-footer">
        <button id="btncancel" class="btn btn-small btn-default">取消</button>
        <button id="btnsave" class="btn btn-small btn-primary">保存</button>
    </div>
    
}
@section scripts{

<script type="text/javascript">


    $(document).ready(function () {

        initValidate();
        //保存
        $("#btnsave").click(function (evt) { evt.preventDefault(); save($(this)); });
        //取消
        $("#btncancel").click(function (evt) { evt.preventDefault(); parent.closeDialog(); return false; });

        $("#colorPicker a").click(function () {
            var color = $(this).attr("data-color");

            $("#colorPicker>button>.span-color").css("background-color", color);
            $("#Bgcolor").val(color);
        });
    });

    //数据验证
    function initValidate() {
        //表单验证
        $('#inputform').validate({
            rules: {
                Deptname: {
                    required: true,
                    maxlength: 30,
                    remote: {
                        url: baseurl + 'Basic/OfficeDept/ExsitCheck',
                        type: 'post',
                        data: {
                            id: $("#Id").val(),
                            deptname: $("#Deptname").val()
                        },
                        dataFilter: function (data, type) {
                            if (data == "yes")
                                return true;
                            else
                                return false;
                        }
                    }
                },
                Sort: { required: true, digits: true },
                Remark: {
                    maxlength: 100
                }
            },
            errorPlacement: function (error, element) {
                var container = element.parents("div.col-sm-6:eq(0)");
                var errorContainer = container.next();
                error.appendTo(errorContainer);
            },
            messages: {
                Roleid: {
                    remote: '科室名称已经存在'
                }
            }
        });
    }

    function save(obj)
    {
        if (!$("#inputform").valid()) {
            return;
        }

        var jsondata = {
            Id: $("#Id").val(),
            Values: {}
        };
     
        //主表数据
        $("#inputform .datafield").each(function () {
            jsondata.Values[$(this).attr("data-fieldname")] = $(this).val();
        });
        //禁用保存按钮，防止重复提交
        $(obj).attr("disabled", true);

        $.ajax({
            type: "post",
            url: baseurl + "Basic/OfficeDept/Save",
            data: {jsondata: JSON.stringify(jsondata) },
            success: function (e) {
                $(obj).attr("disabled", false);
                if (e.Ret == "0") {
                    parent.closeDialog(true);
                } else {
                    alert(e.Msg);
                }
            }
        });
    }

    </script>
}
